package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import modle.Task;

/**
 * Data access object (DAO) for domain model class User.
 * 
 * @see hibernateDao.User
 * @author MyEclipse Persistence Tools
 */

public class TaskDao extends BaseDao {
	// property constants
	public static final String TASK_NAME = "taskName";
	public static final String TASK_INTRODUCTION = "taskIntroduction";
	public static final String PRIORITY = "priority";
	public static final String DEAD_LINE = "deadLine";
	public static final String USER_ID = "userId";

	public int delete(Task persistentInstance) {
		try {
			String sql = "delete from task where taskId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, persistentInstance.getTaskId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	public List<Task> findAll() {
		try {
			String sql = "select * from task";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<Task> findByDeadLine(Object deadLine) {
		return findByProperty(DEAD_LINE, deadLine);
	}

	public Task findById(java.lang.Integer id) {
		try {
			String sql = "select * from task where taskId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, id);
			List<Task> instanceList = this
					.getListByResultSet(ps.executeQuery());
			if (instanceList == null) {
				return null;
			}
			Task instance = instanceList.get(0);
			return instance;
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<Task> findByPriority(Object priority) {
		return findByProperty(PRIORITY, priority);
	}

	public List<Task> findByProperty(String propertyName, Object value) {
		try {
			String sql = "select * from task where " + propertyName + "='"
					+ value + "'";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<Task> findByTaskIntroduce(Object taskIntroduction) {
		return findByProperty(TASK_INTRODUCTION, taskIntroduction);
	}

	public List<Task> findByTaskName(Object taskName) {
		return findByProperty(TASK_NAME, taskName);
	}

	public List<Task> findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	@Override
	protected List<Task> getListByResultSet(ResultSet rs) {
		// TODO Auto-generated method stub
		List<Task> list = new ArrayList<Task>();
		try {
			if (!rs.next()) {
				return null;
			}
			do {
				Task task = new Task();
				task.setTaskId(rs.getInt("taskId"));
				task.setTaskName(rs.getString("taskName"));
				task.setTaskIntroduction(rs.getString("taskIntroduction"));
				task.setPriority(rs.getInt("priority"));
				task.setDeadline(rs.getString("deadLine"));
				list.add(task);
			} while (rs.next());
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}

	public int save(Task transientInstance) {
		try {
			String sql = "insert into task(taskName, taskIntroduction, priority, deadLine, userId) values(?, ?, ?, ?, ?)";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, transientInstance.getTaskName());
			ps.setString(2, transientInstance.getTaskIntroduction());
			ps.setInt(3, transientInstance.getPriority());
			ps.setString(4, transientInstance.getDeadline());
			ps.setInt(5, transientInstance.getUser().getUserId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	@Override
	protected void setSavePreparedStatement(PreparedStatement ps, Object obj)
			throws SQLException {
		// TODO Auto-generated method stub

	}

	public int update(Task instance) {
		try {
			String sql = "update task set taskName=?, taskIntroduction=?, priority=?, deadLine=? where taskId=?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setString(1, instance.getTaskName());
			ps.setString(2, instance.getTaskIntroduction());
			ps.setInt(3, instance.getPriority());
			ps.setString(4, instance.getDeadline());
			ps.setInt(5, instance.getTaskId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}
}